﻿using System.Drawing;
using DotNet.Highcharts;
using DotNet.Highcharts.Enums;
using DotNet.Highcharts.Helpers;
using DotNet.Highcharts.Options;

namespace Home.Utilities
{
    /// <summary>
    /// 图表插件
    /// 官网http://www.highcharts.com
    /// 集成开源http://dotnethighcharts.codeplex.com/
    /// </summary>
    public class HighchartsHelper
    {
        #region 公共静态方法
        /// <summary>
        /// 获取图表效果中的饼图
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="data">数据如：data.Select(d=>new{d.Name,y=d.y}).ToArray(),
        /// 或静态new object[]{new object[] { "Firefox", 45.0 },new object[] { "IE", 26.8 },new Point {Name = "Chrome",Y = 12.8, Sliced = true, Selected = true }}</param>
        /// <returns></returns>
        public static Highcharts GetPie(string title, object[] data)
        {
            Highcharts chart = new Highcharts("chart")
                   .InitChart(new Chart { PlotShadow = false, Margin = new[] { 70, 50, 20, 80 } })//上右下左
                   .SetTitle(new Title { Text = title })
                   .SetTooltip(new Tooltip { Formatter = "function() { return '<b>'+ this.point.name +'</b>: '+ this.point.y; }" })
                //.InFunction("onchart1")
                   .SetPlotOptions(new PlotOptions
                   {
                       Pie = new PlotOptionsPie
                       {
                           AllowPointSelect = true,
                           Cursor = Cursors.Pointer,
                           DataLabels = new PlotOptionsPieDataLabels
                           {
                               Color = ColorTranslator.FromHtml("#000000"),
                               ConnectorColor = ColorTranslator.FromHtml("#000000"),
                               Formatter = "function() { return '<b>'+ this.point.name +'</b>: '+ this.percentage.toFixed(2) +' %'; }"
                           }
                       }
                   })
                   .SetLegend(new Legend { Enabled = false })
                //.SetTooltip(new Tooltip { Enabled = false })
                    .SetExporting(new Exporting { Enabled = false })
                    .SetCredits(new Credits { Enabled = false })//不显示那个网址
                   .SetSeries(new Series
                   {
                       Type = ChartTypes.Pie,
                       //Name = "密级",
                       Data = new Data(data)
                   });
            return chart;
        }
        /// <summary>
        /// 获取图表效果中的柱图
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="yTitle">y轴标题</param>
        /// <param name="xCategories">x轴数据以数组形式</param>
        /// <param name="data">数据如datay.Select(d => new { y = d.y }).ToArray()</param>
        /// <returns></returns>
        public static Highcharts GetColumn(string title, string yTitle, string[] xCategories, object[] data)
        {
            Highcharts chart = new Highcharts("chart")
               .InitChart(new Chart { DefaultSeriesType = ChartTypes.Column, Margin = new[] { 70, 50, 20, 80 } })
               .SetTitle(new Title { Text = title, Y = 40 })
               .SetXAxis(new XAxis
               {
                   Categories = xCategories,
                   Labels = new XAxisLabels
                   {
                       Align = HorizontalAligns.Right,
                       Style = "font: 'normal 13px 宋体'"
                   }
               })
               .SetYAxis(new YAxis
               {
                   Min = 0,
                   Title = new YAxisTitle { Text = yTitle },
                   LineWidth = 1//y轴线，默认没有
               })
               .SetLegend(new Legend { Enabled = false })
               .SetTooltip(new Tooltip { Enabled = false })
               .SetExporting(new Exporting { Enabled = false })
                //.SetTooltip(new Tooltip { Formatter = "function () {return this.y;}" })//格式化函数
               .SetPlotOptions(new PlotOptions
               {
                   Column = new PlotOptionsColumn
                   {
                       DataLabels = new PlotOptionsColumnDataLabels
                       {
                           Enabled = true,
                           // Rotation = -90,//方向
                           //Color = ColorTranslator.FromHtml("#FFFFFF"),
                           //Align = HorizontalAligns.Center,
                           // X = 30,
                           //Y = 0,
                           Formatter = "function() { return this.y; }",
                           Style = "fontWeight: 'bold'"
                       }
                   }
               })
               .SetCredits(new Credits { Enabled = false })//不显示那个网址
               .SetSeries(
                   new Series
                   {
                       // Name = "密级",
                       Data = new Data(data),
                   });
            return chart;
        } 
        #endregion
    }
}
